package com.ocp.macro.handler;

import com.ocp.macro.enums.DataCode;
import com.ocp.macro.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一异常处理
 */
@Slf4j
@ControllerAdvice
public class ExecutorExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public String exceptionHandler(Exception e){
        e.printStackTrace();
        log.debug("出现异常：{}", e.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("code", DataCode.FAIL.getCode());
        map.put("message", DataCode.FAIL.getMessage());
        map.put("debugException", e.toString());
        return GsonUtils.toJson(map);
    }

}
